package com.hspedu.newData.LCR;

import com.hspedu.newData.dmsxl.ds.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-26 
 * @Version: 1.0
 */

public class LCR44在每个树行中找最大值 {

    List<Integer> list = new ArrayList<>();

    public List<Integer> largestValues(TreeNode root) {

        if (root == null) return list;

        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            int len = queue.size();
            int max = queue.peek().val;
            while (len>0){
                TreeNode node = queue.poll();

                if (node.left!=null) queue.offer(node.left);
                if (node.right!=null) queue.offer(node.right);
                if (max<node.val){
                    max = node.val;
                }
                len--;
            }
            list.add(max);
        }
        return list;
    }
}
